/* 
 Project     : 48c6c450f1a4a0cc53d9585dc0fee742
 Created on  : Mar 16, 2013, 11:29:15 PM
 Author      : Truong Khuong - khuongxuantruong@gmail.com
 Description :
 Purpose of the stylesheet follows.
 */
var _CONGIF = {
    JQXGRID_ELEMENT: '#jqxGrid_Backend',
    _TYPE: 'kitchen',
    BINDING_URI: base_url + 'backend/content/binding/',
    EDIT_PANEL_URI: base_url + "backend/content/editpanel",
    COMMIT_URI: base_url + "backend/content/commit/",
    DELETE_URI: base_url + "backend/excution/delete/contents/contents/news",
    PENDING: false
};
var _CONTENT = (function() {
    var init = false;
    var datafields = [
        {name: 'news_id', type: 'int'},
        {name: 'news_title'},
        {name: 'news_alias'},
        {name: 'news_position', type: 'int'},
        {name: 'news_thumb'},
        {name: 'news_status', type: 'bool'},
        {name: 'news_insert'},
        {name: 'news_update'}
    ],
            columns = [
        {text: 'Title', dataField: 'news_title', filterable: false},
        {text: 'Position', dataField: 'news_position', width: 80, filterable: false, cellsalign: 'right', columntype: 'numberinput', filtertype: 'number'},
        {text: 'Status', dataField: 'news_status', width: 80, filterable: false, columntype: 'checkbox', threestatecheckbox: false, filtertype: 'bool'},
        {text: 'Insert', dataField: 'news_insert', width: 120, filterable: false, cellsformat: 'yyyy-MM-dd HH:mm:ss'},
        {
            text: '#', dataField: 'news_id', width: 60, filterable: false, sortable: false,
            cellsrenderer: function(row, column, value) {
                var str = "<span style='margin: 6px; float: left;'>";
                if (value && value > 0) {
                    try {
                        //value = $.parseJSON(value);
                        str += "\
                            <span onclick=\"_CONTENT.EditItem('" + value + "');\" \
                             title='Edit'><img src=\"/libraries/images/16/edit_black.png\"/></span>\
                        ";
                        str += "\
                            <span onclick=\"_CONTENT.DeleteItem('" + value + "');\" \
                             title='Delete'><img src=\"/libraries/images/16/delete_black.png\"/></span>\
                        ";
                    } catch (e) {
                    }
                }
                str += "</span>";
                return str;
            }
        },
    ],
            source, dataAdapter,
            grid_element = _CONGIF.JQXGRID_ELEMENT;
    return {
        OnInit: function() {
            try {
                if (init)
                    return;
                init = true;
                //grid_element=$('#jqxGrid_Backend_Categories');
                source = {
                    datatype: "json", type: "POST",
                    datafields: datafields,
                    url: _CONGIF.BINDING_URI,
                    filter: function() {
                        $(_CONGIF.JQXGRID_ELEMENT).jqxGrid('updatebounddata', 'filter');
                    },
                    sort: function() {
                        $(_CONGIF.JQXGRID_ELEMENT).jqxGrid('updatebounddata');
                    },
                    root: 'rows',
                    beforeprocessing: function(data) {
                        source.totalrecords = data.total_rows;
                    }
                };
                dataAdapter = new $.jqx.dataAdapter(source, {
                    loadError: function(xhr, status, error) {
                        bootbox.alert("<b>Status</b>:" + xhr.status + "<br/><b>ThrownError</b>:" + error + "<br/>");
                    }
                });
                $(_CONGIF.JQXGRID_ELEMENT).jqxGrid({
                    rendergridrows: function(obj) {
                        return obj.data;
                    }, ready: function() {
                        //_CONTENT.Filter('cate_type',_CONGIF._TYPE);

                    },
                    width: '100%', //autoheight:true,
                    source: dataAdapter,
                    theme: theme, sortable: true,
                    //filterable: true,
                    //autoshowfiltericon: true,
                    //showfilterrow: true,
                    //rowsheight:34,
                    showemptyrow: true,
                    virtualmode: false,
                    columns: columns
                });
            } catch (e) {
                bootbox.alert(e.message);
            }
        },
        AddItem: function() {
            this.EditItem(0);
        },
        EditItem: function(Id) {
            //if(PENDING) return false;
            //_CONGIF.PENDING=true;

            new backend({
                url: _CONGIF.EDIT_PANEL_URI,
                data: {
                    Id: Id,
                    //Type    :   _CONGIF._TYPE
                },
                callback: function(rsdata) {
                    //_CONGIF.PENDING=false;
                    if (rsdata.result < 0) {
                        bckdialog({
                            message: rsdata.message,
                            type: 'error'
                        }).open();
                    } else {
                        $('#jqxGrid_Backend').hide();
                        $('#_edit_panel').html(rsdata.htmlreponse).show();
                        $('#news_category').selectpicker({});
                        $('#news_page').selectpicker({});
                        CKEDITOR.replace('news_content');
                    }
                }
            }).call();
        },
        Save: function() {
            if (_CONGIF.PENDING)
                return false;
            //if( $('#category_form_edit_panel').validationEngine('validate') === false)return false;
            var Params = $('#_form_edit_panel').serializeObject();
            Params.news_content = CKEDITOR.instances['news_content'].getData();
            //console.log(Params);return;
            _CONGIF.PENDING = true;
            new backend({
                url: _CONGIF.COMMIT_URI,
                data: {
                    Params: Params,
                    content: Params.news_content,
                    Id: $('#news_id').val()
                },
                callback: function(rsdata) {
                    _CONGIF.PENDING = false;
                    if (rsdata.result < 0) {
                        $.jGrowl(rsdata.message, {sticky: true, theme: 'growl-error', header: 'Error!'});
                        return false;
                    } else {
                        $.jGrowl(rsdata.message);
                    }
                }
            }).call();
            return false;
        },
        SaveById: function(Id) {
            if (_CONGIF.PENDING)
                return false;
            _CONGIF.PENDING = true;
            new backend({
                url: _CONGIF.COMMIT_URI,
                data: {
                    Params: {
                        news_title  :   $('#news_title_' + Id).val(),
                        news_content:   CKEDITOR.instances['news_content_' + Id].getData()
                    },
                    Id: Id
                },
                callback: function(rsdata) {
                    _CONGIF.PENDING = false;
                    if (rsdata.result < 0) {
                        $.jGrowl(rsdata.message, {sticky: true, theme: 'growl-error', header: 'Error!'});
                        return false;
                    } else {
                        $.jGrowl(rsdata.message);
                    }
                }
            }).call();
            return false;
        },
        Cancel: function() {
            $('#jqxGrid_Backend').show();
            $('#_edit_panel').hide();
        },
        DeleteItem: function(id) {
            ConfirmMsg(
                    function() {
                        backend_delete('contents', 'contents', 'news', id, function() {
                            $(_CONGIF.JQXGRID_ELEMENT).jqxGrid('updatebounddata');
                            //Example.show("Success ! Item have been Deleted.");
                        });
                    }
            , "These items will be permanently deleted and cannot be recovered. Are you sure ?"

                    );
        },
        RestoreItem: function() {
            accessdenied();
        },
        Permission: function(id) {

        },
        PrivateItem: function() {
            accessdenied();
        },
        Setting: function() {
            bckdialog({
                message: $('#window-sOpt'),
                icon: "<img class='p-a t-7 l-8' src='" + base_url + "/libraries/images/16/option.gif'/>",
                title: 'Setting'
            }).open();
        },
        Refresh: function() {
            $(_CONGIF.JQXGRID_ELEMENT).jqxGrid('updatebounddata');
            //Msg('✔ Dép bông nhung mềm mịn - 68.000 | Bộ màu vẽ 68 món - 85.000 | Máy uốn tóc new - 135.000');
        },
        OtherAdvand: function() {
            $('.otheradvand').toggle();
        },
        Filter: function(datafield, filtervalue) {
            _CONGIF._TYPE = filtervalue;
            var filtertype = 'stringfilter';
            var filtergroup = new $.jqx.filter();
            var filter_or_operator = 1;
            var filtercondition = 'equal';
            var filter = filtergroup.createfilter(filtertype, filtervalue, filtercondition);
            filtergroup.addfilter(filter_or_operator, filter);

            //$(grid_element).jqxGrid('clearfilters');
            // add the filters.
            $(_CONGIF.JQXGRID_ELEMENT).jqxGrid('addfilter', datafield, filtergroup);
            // apply the filters.
            $(_CONGIF.JQXGRID_ELEMENT).jqxGrid('applyfilters');
        }
    };
}());
$(document).ready(function() {
    $('#_edit_panel').hide();
});